﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.User;
using Newtonsoft.Json;

namespace WebApplication.User
{
    public partial class Handle : BaseHandle
    {
        private UserService userService = new UserService();

        public void UpdateAvatar()
        {
            string avatarUrl = Request.Form["url"];
            int uid = Convert.ToInt32(Session["UID"]);
            Models.UserInfo userInfo = userService.GetUserInfo(new Models.User { UserID = uid });
            userInfo.AvatarUrl = avatarUrl;
            if (!userService.updateUserInfo(userInfo))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "更新用户头像失败！" }));
                return;
            }
            Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "更新用户头像成功！" }));
        }

        public void UpdateUserInfo()
        {
            int uid = Convert.ToInt32(Session["UID"]);
            Models.UserInfo userInfo = userService.GetUserInfo(new Models.User { UserID = uid });

            Models.UserInfo formUserInfo = new Utils.Model<Models.UserInfo>().GetModel(Request.Form);
            formUserInfo.UserInfoID = userInfo.UserInfoID;

            formUserInfo.AvatarUrl = userInfo.AvatarUrl; // 由于截取了前面的~，获取这里修改要补上

            if (!userService.updateUserInfo(formUserInfo))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "更新用户信息失败！" }));
                return;
            };
            Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "更新用户信息成功！" }));

        }

        public void UpdateResumeInfo()
        {
            int uid = Convert.ToInt32(Session["UID"]);

            Models.ResumeInfo resumeInfo = userService.GetResumeInfo(new Models.User { UserID = uid });

            Models.ResumeInfo formResumeInfo = new Utils.Model<Models.ResumeInfo>().GetModel(Request.Form);
            formResumeInfo.ResumeID = resumeInfo.ResumeID;

            if (!userService.updateResumeInfo(formResumeInfo))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "更新用户接稿信息失败！" }));
                return;
            }
            Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "更新用户接稿信息成功！" }));

        }

        public void Register()
        {
            Models.User user = new Utils.Model<Models.User>().GetModel(Request.Form);

            if (userService.ExistUserByUserName(user.UserName) != null)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "用户名已经存在！" }));
                return;
            }

            Models.UserInfo userInfo = new Models.UserInfo();

            Models.ResumeInfo resumeInfo = new Models.ResumeInfo();

            userService.createUser(user, userInfo, resumeInfo);

            Session["UID"] = user.UserID;
            Session["USER"] = user;

            Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "注册成功！" }));
        }

        public void Login()
        {
            Models.User user = new Utils.Model<Models.User>().GetModel(Request.Form);

            if ((user = userService.ExistUserByUserNameAndPwd(user.UserName, user.UserPwd)) == null)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "用户名或密码错误！" }));
                return;
            }


            Session["UID"] = user.UserID;
            Session["USER"] = user;
            Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "登陆成功！" }));
        }
    }
}